package util;

import java.util.*;

public class Scope {
	Scope upper;
	Map<String, Reference> references = new HashMap<String, Reference>();
	
	public Scope(Scope upper) {
		super();
		this.upper = upper;
	}
	
	public void addReference(Reference reference){
		references.put(reference.getName(), reference);
	}
	
	public Reference getReference(String name){
		if(references.containsKey(name)){
			return references.get(name);
		}else if(upper == null){
			return null;
		}else{
			return upper.getReference(name);
		}
	}
	
	public String toString() {
		StringBuffer str = new StringBuffer();
		str.append("References: [");
		Set<String> set = references.keySet();
		for(String el: set) {
			str.append("["+references.get(el).toString()+"], ");
		}
		str.append("], rootScope: ["+((upper==null)?"none":upper.toString())+"]");
		return str.toString();
	}
}
